<?php
/**
 * 用户收藏
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/12/21
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\Db;
class UserCollectModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //禁用
    const DEL_STATUS=-1; //删除

    protected $autoWriteTimestamp = true;

    public function userCar(){
        return $this->hasOne('UserCarModel','id','user_car_id')->field('id,user_id,price,mileage,card_time');
    }

    public function car(){
        return $this->hasOne('CarModel','car_id','car_id')->field('id,car_id,car_name,series_thumb,series_mine');
    }

    /**
     * 所有列表
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('car')
            ->where($where)
            ->order('create_time desc')
            ->paginate($pageSum);

        $field='car_id,series_thumb,car_name,car_pl_l,car_bsxlx,car_hbbz';

        foreach ($list as $item){
            $uc=UserCarModel::field("id,series_id,card_time,mileage,price,good_numb")->get($item->user_car_id);

            if(!empty($item->car_id)){
                $item['info']=CarModel::field($field)->where('car_id',$item->car_id)->find();
                $item['info']["card_time"]=$uc->card_time;
                $item['info']["mileage"]=$uc->mileage;
                $item['info']["price"]=$uc->price;

            }else{
                $cs=CarSeriesModel::field("series_name,thumb")->where('series_id',$uc->series_id)->find();
                $item['info']=$cs;
                $item['info']["car_name"]=$cs->series_name;
                $item['info']["series_thumb"]=$cs->thumb;
                $item['info']["card_time"]=$uc->card_time;
                $item['info']["mileage"]=$uc->mileage;
                $item['info']["price"]=$uc->price;
            }
            $item['info']["good_numb"]=$uc["good_numb"];
            $item['info']["collectCount"]=self::field("id")->where('user_car_id',$uc->id)->where("status",self::SHOW_STATUS)->count();
            $item['info']["msgCount"]=UserCarMessageModel::field("id")
                ->where('user_car_id',$uc->id)
                ->where("status",UserCarMessageModel::SHOW_STATUS)
                ->count();

        }

        return $list;
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $status=self::destroy($id);
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * 增加
     * @param $data
     * @return array
     */
    public function adds($data){
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'已存在！'];
        }

        $status = self::save($data);

        if($status){
            return['status'=>1,'msg'=>'增加成功！'];
        }else{
            return['status'=>0,'msg'=>'增加失败！'];
        }
    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){

        $count=self::where('user_id',$data['user_id'])
            ->where('user_car_id',$data['user_car_id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取用户收藏
     * @param $uid
     * @param $status
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUserCollect($uid,$pageSum = 0){
        if ($pageSum <= 0) {
            $pageSum = getSystem("pageSUM") ? getSystem("pageSUM") : 10;
        }

        $list = self::view("UserCollect","id as coll_id,user_car_id,car_id")
            ->view("UserCar","id as uc_id,vin,img_list,sell_status,good_numb,price,mileage,card_time,series_id","UserCollect.user_car_id = UserCar.id")
            ->where(["UserCollect.status"=>self::SHOW_STATUS,"UserCollect.user_id"=>$uid])
            ->order("UserCar.sell_status asc,UserCollect.create_time desc")
            ->paginate($pageSum);

        foreach ($list as $item){
            $item["img_list"] = json_decode($item["img_list"], true);
            $item["collectCount"] = self::where('user_car_id', $item["user_car_id"])
                ->where("status", self::SHOW_STATUS)
                ->count();

            $item["msgCount"] = UserCarMessageModel::where('user_car_id', $item["user_car_id"])
                ->where("status", UserCarMessageModel::SHOW_STATUS)
                ->count();

            if(!empty($item->vin)){
                $cs=CarSeriesModel::field("series_name,thumb")->where('series_id',$item->series_id)->find();
                $carVin=CarLicenseMoreModel::field("sale_name")->where('vin','=',$item->vin)->find();
                $item['car']=[
                    "car_name"=>$carVin->sale_name,
                    "series_thumb"=>$cs->thumb
                ];
            }else{
                if(!empty($item->car_id)){
                    $item['car']=CarModel::field("car_id,car_name,series_thumb")->where('car_id',$item->car_id)->find();
                }else{
                    $cs=CarSeriesModel::field("series_name,thumb")->where('series_id',$item->series_id)->find();

                    $item['car']=[
                        "car_name"=>$cs->series_name,
                        "series_thumb"=>$cs->thumb
                    ];
                }
            }


        }

        return $list;
    }

    /**
     * 降价通知
     * @param $uid
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCutPrice($uid,$pageSum = 0){
        if ($pageSum <= 0) {
            $pageSum = getSystem("pageSUM") ? getSystem("pageSUM") : 10;
        }
        $list = Db::view("userCollect","*")
            ->view("userCar","id,user_id,price,old_price,mileage,card_time","userCollect.user_car_id = userCar.id")
            ->view("car","car_name,series_thumb,series_mine","userCar.car_id = car.car_id")
            ->where(["userCollect.user_id"=>$uid])
            ->where("userCar.price < userCar.old_price")
            ->paginate($pageSum);
        return $list;
    }
}